<template>
    <section>
        <el-form :model="User" ref="User" :rules="rule" label-width="100px" class="logIn">
            <h2>后台管理系统登陆</h2>
            <el-form-item label="用户名" prop="email">
                <el-input v-model="User.email"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="User.password"></el-input>
            </el-form-item>
            <el-button @click="logIn">登陆</el-button>
        </el-form>
    </section>
</template>
<script>
    /* eslint-disable indent,space-before-function-paren */
    import {LogIn} from '../api/api'

    export default {
        data() {
            return {
                User: {
                    email: '111',
                    password: '111'
                },
                rule: {
                    email: [
                        {required: true, message: '请输入邮箱', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            logIn() {
                this.$refs['User'].validate((valid) => {
                    if (valid) {
                        LogIn(
                            {
                                email: this.User.email,
                                password: this.User.password
                            }
                        ).then(
                            data => {
                                if (data.status) {
                                    console.log('登陆成功')
                                    window.sessionStorage.setItem('token', data.token)
                                    window.sessionStorage.setItem('name', data.data.name)
                                    this.$router.push({path: '/home'})
                                }
                            }
                        ).catch()
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            }
        }
    }
</script>

<style>
    .logIn {
        width: 500px;
        position: absolute;
        top: 40%;
        left: 50%;
        transform: translate(-50%, -50%);
        box-shadow: 0 0 20px 1px #00000021;
        padding: 20px;
        border-radius: 5px;
    }
</style>
